<?php	require_once(dirname(__FILE__).'/inc/config.inc.php');IsModelPriv('member');

/*
**************************
(C)2010-2015 phpMyWind.com
update: 2014-5-30 17:16:14
person: Feng
**************************
*/


//初始化参数
$tbname = '#@__member';
$gourl  = 'member.php';


//引入操作类
require_once(ADMIN_INC.'/action.class.php');


//添加会员
if($action == 'add')
{
	if(!isset($enteruser)) $enteruser = '';

	if(preg_match("/[^0-9a-zA-Z_@!\.-]/",$username) || preg_match("/[^0-9a-zA-Z_@!\.-]/",$password))
	{
		ShowMsg('用户名或密码非法！请使用[0-9a-zA-Z_@!.-]内的字符！','-1');
		exit();
	}
	if($password != $repassword)
	{
		ShowMsg('两次输入的密码不一样！','-1');
		exit();
	}

	$r = $dosql->GetOne("SELECT username FROM `$tbname` WHERE username='$username'");
	if(!empty($r['username']))
	{
		ShowMsg('用户名已存在！','-1');
		exit();
	}

	$password = md5(md5($password));
	$regtime  = GetMkTime($regtime);
	$regip    = GetIP();

	$sql = "INSERT INTO `$tbname` (username, password, question, answer, cnname, enname, sex, birthtype, birth_year, birth_month, birth_day, astro, bloodtype, trade, live_prov, live_city, live_country, home_prov, home_city, home_country, cardtype, cardnum, intro, email, qqnum, mobile, telephone, address_prov, address_city, address_country, address, zipcode, enteruser, expval, integral, regtime, regip, logintime, loginip) VALUES ('$username', '$password', '$question', '$answer', '$cnname', '$enname', '$sex', '$birthtype', '$birth_year', '$birth_month', '$birth_day', '$astro', '$bloodtype', '$trade', '$live_prov', '$live_city', '$live_country', '$home_prov', '$home_city', '$home_country', '$cardtype', '$cardnum', '$intro', '$email', '$qqnum', '$mobile', '$telephone', '$address_prov', '$address_city', '$address_country', '$address', '$zipcode', '$enteruser', '$expval', '$integral', '$regtime', '$regip', '$regtime', '$regip')";
	if($dosql->ExecNoneQuery($sql))
	{
		header("location:$gourl");
		exit();
	}
}


//修改会员信息
else if($action == 'update')
{
	if(!isset($enteruser)) $enteruser = '';

	// if($password != $repassword)
	// {
	// 	ShowMsg('两次输入的密码不一样！','-1');
	// 	exit();
	// }
	if($is_vip == 2 || $is_vip == 3){
		if($start_time == '' || $end_time == ''){
			ShowMsg('身份为个人或商户时开始和结束日期为必填项','-1');
			exit();
		}
	}
	if($ago_is_vip == 4 && $is_vip == 3 ){
		ShowMsg('业务员身份不可转换为商户','-1');
		exit();
	}

	//删除头像
	if(!empty($delavatar))
	{
		$avatarsize = array(1 => 'big', 2 => 'middle', 3 => 'small');
		foreach($avatarsize as $size)
		{
			file_exists(PHPMYWIND_DATA.'/avatar/'.get_avatar_filepath($id,$size)) &&
			unlink(PHPMYWIND_DATA.'/avatar/'.get_avatar_filepath($id,$size));
		}
	}

	$sql = "UPDATE `$tbname` SET ";
	if($password != '')
	{
		$password = md5(md5($password));
		$sql .= "password='$password', ";
	}
	$start_time = strtotime($start_time);
	$end_time = strtotime($end_time);
	if($industry_type){
	    $sql .= "industry_type ='$industry_type',";
    }
	$sql .= "question='$question', answer='$answer', cnname='$cnname',avatar='$avatar', enname='$enname',is_vip='$is_vip',start_time='$start_time',end_time='$end_time', sex='$sex', birthtype='$birthtype', birth_year='$birth_year', birth_month='$birth_month', birth_day='$birth_day', astro='$astro', bloodtype='$bloodtype', trade='$trade', live_prov='$live_prov', live_city='$live_city', live_country='$live_country', home_prov='$home_prov', home_city='$home_city', home_country='$home_country', cardtype='$cardtype', cardnum='$cardnum', intro='$intro', email='$email', qq_email='$qq_email', mobile='$mobile', telephone='$telephone', address_prov='$address_prov', address_city='$address_city', address_country='$address_country', address='$address', zipcode='$zipcode', enteruser='$enteruser', expval='$expval', integral='$integral', label='$label', merchant_num='$merchant_num', merchant_key='$merchant_key' WHERE id=$id";
	$salesman = $dosql->GetOne("select * from dyspw_salesman where u_id = $id");
	$member = $dosql->GetOne("select * from dyspw_member where id = $id");

 	//开启事务
    mysqli_autocommit($dosql->linkid,FALSE);
	$print_bool=true;
    //添加打印机型号
    if($print_name && $print_sn && $print_key && $print_type){
        $obj = new printTicket();
        $result=$obj->printAdd([
            [$print_sn,$print_key,$print_name]
        ],$print_type);
        if(!$result['status']){
            ShowMsg($result['data'],'-1');
            exit();
        }else{
            if($result['data']['data']['no']){
                ShowMsg(implode(';',$result['data']['data']['no']),'-1');
                exit();
            }else{
				//更新关联表
                $print_sql="INSERT INTO sys_printer(uid,print_sn,print_key,print_name,create_time,print_type) VALUES('$id','$print_sn',
'$print_key','$print_name',".time().",'$print_type') ON DUPLICATE KEY UPDATE print_sn='$print_sn',print_key='$print_key',print_name='$print_name',update_time = ".time().",print_type = ".$print_type;
                $print_bool=$dosql->ExecNoneQuery($print_sql);
            }
        }
    }
   	$update_bool=$dosql->ExecNoneQuery($sql);

	if($update_bool && $print_bool){
        mysqli_commit($dosql->linkid);
		//判定该角色之前角色不为商户
		// if($ago_is_vip != 3 && $is_vip == 3 && !empty($member['invite_code'])){
		// 	$time = time();
		// 	$salesman= $dosql->GetOne("select * from dyspw_salesman where invite_code = ".$member['invite_code']);
		// 	if($salesman['parent_id'] != 0){
		// 		//父级id不为空时给父级id添加分账
		// 		$info = $salesman['u_name'].'邀请'.$member['username'].'店铺入驻';
		// 		$dosql->ExecNoneQuery("INSERT INTO dyspw_sale_cash (s_id, s_money, info, `time`) VALUES ({$salesman['parent_id']},30,'$info',$time)");
		// 		$dosql->ExecNoneQuery("UPDATE dyspw_salesman SET cash = cash+30, expect_cash = expect_cash+300 WHERE `id`={$salesman['parent_id']}");
		// 	}
		// 	if($salesman['forbears_id'] != 0){
		// 		$info = $salesman['u_name'].'邀请'.$member['username'].'店铺入驻';
		// 		$dosql->ExecNoneQuery("INSERT INTO dyspw_sale_cash (s_id, s_money, info, `time`) VALUES ({$salesman['forbears_id']},20,'$info',$time)");
		// 		$dosql->ExecNoneQuery("UPDATE dyspw_salesman SET cash = cash+20, expect_cash = expect_cash+200 WHERE `id`={$salesman['forbears_id']}");
		// 	}
		// 	$info = '邀请'.$member['username'].'店铺入驻';
		// 	$dosql->ExecNoneQuery("INSERT INTO dyspw_sale_cash (s_id, s_money, info, `time`) VALUES ({$salesman['id']},100,'$info',$time)");
		// 	$dosql->ExecNoneQuery("UPDATE dyspw_salesman SET cash = cash+100, expect_cash = expect_cash+1000 WHERE `id`={$salesman['id']}");
		// }

		if(empty($salesman) && $is_vip == 4){//后台直接审核成为顶级业务员
			$rand = rand(9999999,1000000);
			$invite_code = $rand . $id;
			$time = time();
			//生成业务员邀请码二维码流程
			require_once "../phpqrcode.php";
			define('UPLOAD_PATH', '../images/');
			define('UPLOAD_URL', '/xiaochengxu/images/qr_code/orders_hexiao/');
			//二维码流程
			$file_path =  './images/qr_code/salesman_code/';
			$qr_code_path = UPLOAD_PATH . 'qr_code/orders_hexiao/';//二维码保存的路径信息
			$sale_url = 'https://'.$_SERVER['HTTP_HOST']."/xiaochengxu/salesman_up.php?invite_code=".$invite_code;//定义二维码包含的文字信息内容
			$shop_url = 'https://'.$_SERVER['HTTP_HOST']."/xiaochengxu/shop_edit.php?invite_code=".$invite_code;//定义二维码包含的文字信息内容
			$shop_url_file = $qr_code_path . $invite_code . "shop.png";
			$sale_url_file = $qr_code_path . $invite_code . "sale.png";
			$size = 5;
			if(!\QRcode::png($sale_url, $sale_url_file, QR_ECLEVEL_M, $size)){//png方法生成二维码并保存至指定文件夹
				$invite_img = 'https://'.$_SERVER['HTTP_HOST'] . UPLOAD_URL . $invite_code . "sale.png";
			}
			if(!\QRcode::png($shop_url, $shop_url_file, QR_ECLEVEL_M, $size)){//png方法生成二维码并保存至指定文件夹
				$invite_shop = 'https://'.$_SERVER['HTTP_HOST'] . UPLOAD_URL . $invite_code . "shop.png";
			}
			$dosql->ExecNoneQuery("INSERT INTO dyspw_salesman (u_id, u_name, u_avatar, invite_code, parent_id, u_intro,invite_img,invite_shop, state,`time`) VALUES ('$id', '{$member['username']}', '{$member['avatar']}',$invite_code, 0, '{$member['intro']}','$invite_img','$invite_shop',1,$time)");
		}elseif (!empty($salesman) && $is_vip == 4 && $salesman['state'] == 0) {//改变审核业务员状态信息
			$dosql->ExecNoneQuery("UPDATE dyspw_salesman SET invite_num = invite_num+1 WHERE `id`={$salesman['parent_id']}");
			$dosql->ExecNoneQuery("UPDATE dyspw_salesman SET state = 1 WHERE `id`={$salesman['id']}");
		}
		header("location:$gourl");
		exit();
	}else{
        mysqli_rollback($dosql->linkid);
        ShowMsg('信息更新失败','-1');
        exit();
	}
}


//移除绑定QQ
else if($action == 'removeoqq')
{
	$dosql->ExecNoneQuery("UPDATE `#@__member` SET `qqid`='' WHERE `id`='$id'");
	ShowMsg('解除QQ绑定成功！','member_update.php?id='.$id);
	exit();
}


//移除绑定微博
else if($action == 'removeoweibo')
{
	$dosql->ExecNoneQuery("UPDATE `#@__member` SET `weiboid`='' WHERE `id`='$id'");
	ShowMsg('解除微博绑定成功！','member_update.php?id='.$id);
	exit();
}


//无条件返回
else
{
    header("location:$gourl");
	exit();
}
?>
